package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	6186
 * http://www.wuhunews.cn/  芜湖新闻网
 *
 * @author KK
 *
 */
public class WuhunewsNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/

		"http://www.wuhunews.cn/[a-z]*/[a-z]*/[0-9-0-9-0-9]*/[0-9]*.html",
//		"http://[a-z]*.wuhunews.cn/[a-z0-9]*/[a-z0-9]*/[0-9]*/[0-9]*/[0-9]*-[0-9]*-[0-9]*.html",
//		"http://[a-z]*.wuhunews.cn/[a-z0-9]*/[a-z0-9]*/[a-z0-9]*/[0-9]*/[0-9]*/[0-9]*-[0-9]*-[0-9]*.html",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);
		if(doc.select("error:contains(没有找到要指定的内容，此内容不存在或已被删除)").size()>0){
			news.setUpdateUrl(ParseState.ERR404.toString());
			return news;
		}
		if(url.startsWith("http://www.wuhunews.cn/pic")){
			title=doc.select("div.straarticle-titlenge h1").text();
			date=DateUtils.matchDate(StringUtils.substringAfterLast(url,"/"));
			content=HtmlCleaner.getContentHtml(url,doc.select("div#gallery p"));
		}else{
			title =doc.select(".Title").text();
			if (StringUtils.isBlank(title)){
				title =doc.select("title").text();
				if (title!=null)title=StringUtils.substringBefore(title, "_");
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div#ContentText"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url, doc.select("div.Content"));
			}
			if(StringUtils.isBlank(content)){
				Elements select=doc.select("div#content3j");
				if(select.size()>0){
					content=select.get(0).ownText();
				}
			}

			/*
			 * 作者
			 */
			author=JSoupUtils.matchAuthor(doc, "来源:");

			date=DateUtils.matchDate(doc.select("span.time").text());
			if(date==null){
				date=JSoupUtils.matchDate(doc, "发布时间");
				date=DateUtils.matchDate(doc.select("div:contains(发布时间)").text());
				if(date==null){
					date=DateUtils.matchDate(StringUtils.substringAfterLast(url,"/"));
				}
			}
		}
		if (StringUtils.isNotBlank(title) && date!=null){
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);
			news.setType(1);
		}
		return news;
	}
	public static void main(String[] args) {
		String url="http://www.wuhunews.cn/wuhu/daily/2016-10-07/1058520.html";
		WuhunewsNewsAnalyse analyse=new WuhunewsNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
